package y2016.date1203.pm.古诗文网采集;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;

public class TextTool {

    public static String readFileString(String path, String charset) {
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(path);
            InputStream in = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(isr);
            char[] ch = new char[1024];
            int len = 0;

            while ((len = br.read(ch)) != -1) {
                sb.append(new String(ch, 0, len));
            }
            br.close();
            isr.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();


    }

    /**
     * @param htmlText 待写入的html
     * @param delete   要删除的文字
     * @param path     保存的路径,分隔符为“/”
     * @param charset  编码方式
     * @throws IOException
     */
    public static void writeFormatToFile(String htmlText, String[] delete, String path, String charset) {
        String txt = formatTxt(htmlText, delete);
        writeTextToFile(txt, delete, path, charset);

    }

    public static void writeTextToFile(String txt, String[] delete, String path, String charset) {

        File file = new File(path);
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {

            if (!file.exists()) {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            }
            os = new FileOutputStream(file, true);//追加的写入方式
            osw = new OutputStreamWriter(os, charset);
            bw = new BufferedWriter(osw);
            bw.write(txt);

            bw.flush();
            osw.flush();
            os.flush();

            bw.close();
            osw.close();
            os.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String formatTxt(String htmlText, String[] delete) {
        Document doc = Jsoup.parse(htmlText);
        Elements eTitles = doc.getElementsByTag("h1");
        String title = eTitles.text();
        Elements elements = doc.getElementsByAttributeValue("class", "bookvson2");
        System.out.println(eTitles.text());
        String content = elements.text();
        if (delete != null) {
            for (int i = 0; i < delete.length; i++) {
                String string = delete[i];
                content = content.replace(string, "");
            }
        }

        content = content.replace(" 　　", "\n　　");

        return (new StringBuilder(title + "\n" + content + "\n\n\n")).toString();

    }


}

